#include "global.h"
#include "serial.h"
#include "uart2.h"
#include "cmdline.h"

void uart_tx(char data)
{
    while (!(UCSR0A & (1<<UDRE)));
    UDR0 = data;
}

char uart_rx(void)
{
	while ( !(UCSR0A & (1<<RXC0)) );
	return UDR0;
}

//flags

u08 data_received;

u16 data_count=0;
u08 header_found;

void serialInit(void)
{
	if (CAM_UART == 0)
		uart0Init();
	else
		uart1Init();
		
    uartSetBaudRate(CAM_UART, 115200);
	uartSetRxHandler(CAM_UART, rxDataHandler);
	
	return;
}

void putchr(char c)
{

  loop_until_bit_is_set(UCSRA, UDRE);
  UDR = c;
}

// Send a C (NUL-terminated) string down the UART Tx.
void printstr(const char *s)
{
  while (*s)
    {
      if (*s == '\n') putchr('\r');
      putchr(*s++);
    }
}

void serialTest(void)		
{
	uart0SendByte(0xaa);
	uart0SendByte(0x0a);
}

void rxDataHandler(u08 data)										//< interrupt-based incoming data handler
{
	//u16 rx_data;  
    //uart0SendByte(data);
	
	cmdlineInputFunc(data);

}

